package app.server.project.cloud.random.service;

import app.server.project.cloud.random.db.dao.DeviceBaseDao;
import app.server.project.cloud.random.dto.auth.WebLoginResp;
import app.server.project.core.constants.CfgDef;
import app.server.project.core.constants.Def;
import app.server.project.core.exception.SysError;
import app.server.project.core.token.TokenCache;
import app.server.project.core.util.AssertUtil;
import app.server.project.cloud.random.db.dao.AccountDao;
import app.server.project.cloud.random.db.entity.AccountEntity;
import app.server.project.cloud.random.dto.auth.AppLoginReq;
import app.server.project.cloud.random.dto.auth.AppLoginResp;
import app.server.project.cloud.random.dto.auth.WebLoginReq;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.MD5;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class AuthService {

    private final static String DEF_CODE="1234";


    @Value("${" + CfgDef.chkToken + ":" + false + "}")
    private boolean chkToken;

    @Autowired
    private EmailService emailService;
    @Autowired
    private TokenCache tokenCache;
    @Autowired
    private AccountDao accountDao;
    @Autowired
    private DeviceBaseDao deviceBaseDao;

    /**
     * app 发送验证码
     * @param email
     */
    public void sendCode(String email){
        String code;
        if (this.chkToken){
            code=String.valueOf(RandomUtil.getRandom().nextInt(1000,9999));
        }else {
            code=DEF_CODE;
        }
        String token= MD5.create().digestHex(code+ Def.TokenKey);
        AccountEntity accountEntity=this.accountDao.getDataByEmail(email);
        this.tokenCache.addToken(accountEntity.getUserId(), token);
        if (this.chkToken){
            this.emailService.sendCode(email, code);
        }

    }

    /**
     * app 登录
     * @param
     */
    public AppLoginResp appLogin(AppLoginReq req){
        AssertUtil.isTrue(StrUtil.isNotBlank(req.getEmail()),SysError.EmptyParam);
        AssertUtil.isTrue(StrUtil.isNotBlank(req.getCode()),SysError.EmptyParam);
        AccountEntity accountEntity=this.accountDao.getDataByEmail(req.getEmail());
        String token=null;
        if (!chkToken){
            token= MD5.create().digestHex(req.getCode()+ Def.TokenKey);
            this.tokenCache.addToken(accountEntity.getUserId(), token);
        }else {
            token=this.tokenCache.getToken(accountEntity.getUserId());
        }
        AssertUtil.isTrue(StrUtil.isNotBlank(token), SysError.Code);
        return AppLoginResp.of(accountEntity.getUserId(), token);
    }

    /**
     * 设备获取token
     * @param devId
     * @return
     */
    public AppLoginResp devGetToken(long devId){
        //目前先不检验设备
        var token=this.tokenCache.addToken(devId,MD5.create().digestHex(String.valueOf(devId)));
        return AppLoginResp.of(devId, token);
    }

    public WebLoginResp webGetToken(WebLoginReq req){
        //进行简单的权限检查,默认写死密码
        long userId=10L;
        String userName="iot";
        String pwd="iot";
        AssertUtil.isTrue(userName.equals(req.getUserName())&&pwd.equals(req.getPwd()),Def.Zero);
        String token=MD5.create().digestHex(String.valueOf(RandomUtil.getRandom().nextInt()));
        this.tokenCache.addToken(userId,token);
        return WebLoginResp.of(userId,token);
    }
}
